<?php
namespace App\Service\Admin;

use Illuminate\Support\Facades\DB;

class MkWordService extends BaseService
{
    //功能库

    //功能选项
    public function getOption()
    {
        $arys['pid'][] = ['id' =>0, 'title' => '顶级功能'];
        $res = $this->dbConn($this->dbMkWord)
            ->select('id', 'cn')
            ->where('del', 0)
            ->where('pid', 0)
            ->get();
        if (!empty($res)) {
            foreach ($res as $v) {
                $v = (array) $v;
                $arys['pid'][] = [
                    'id' => $v['id'],
                    'title' => $v['cn'],
                ];
            }
        }
        return $this->toback(0, '', $arys);
    }

    /**
     * 列表
     * @param array $pages【start，limit】
     */
    public function getIndex($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbMkWord)
            ->where('del', 0)
            ->where('pid', 0);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (empty($res)) {
            return $this->toback(-2, '没有功能', [], $total);
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $childs = $this->dbConn($this->dbMkWord)
                ->select('id', 'cn as title')
                ->where('del', 0)
                ->where('pid', $v['id'])
                ->get();
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['cn'],
                'childList' => $childs,
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    //新增
    public function add($pas)
    {
        $resOne = (array) $this->dbConn($this->dbMkWord)
            ->where('del', 0)
            ->where('cn', $pas['cn'])
            ->where('pid', $pas['pid'])
            ->first();
        if (!empty($resOne)) {
            return $this->toback(-2, '该功能已存在');
        }
        $pas['en'] = toFieldName();
        $pas['addtime'] = toDate();
        $id = $this->dbInsertOne($this->dbMkWord, $pas);
        if (!$id) {
            return $this->toback(-1, '添加失败');
        }
        return $this->toback();
    }

    //修改
    public function modify($id, $pas)
    {
        $resOne = (array) $this->dbConn($this->dbMkWord)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($resOne)) {
            return $this->toback(-2, '该功能不存在');
        }
        $resOne = (array) $this->dbConn($this->dbMkWord)
            ->where('del', 0)
            ->where('cn', $pas['cn'])
            ->where('id', '<>', $id)
            ->first();
        if (!empty($resOne)) {
            return $this->toback(-2, '存在同名功能');
        }
        $pas['uptime'] = toDate();
        $up = $this->dbConn($this->dbMkWord)->where('id', $id)->update($pas);
        if (!$up) {
            return $this->toback(-1, '修改失败');
        }
        return $this->toback();
    }

    //删除
    public function del($id)
    {
        $resOne = (array) $this->dbConn($this->dbMkWord)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($resOne)) {
            return $this->toback(-2, '该功能不存在');
        }
        $ays = [
            'del' => 1,
            'deltime' => toDate(),
        ];
        $up = $this->dbConn($this->dbMkWord)->where('id', $id)->update($ays);
        if (!$up) {
            return $this->toback(-1, '删除失败');
        }
        return $this->toback();
    }
}